home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / VectorInsertAndErase.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  976 b   |  35 lines

  1. //: C20:VectorInsertAndErase.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Erasing an element from a vector
  7. #include "Noisy.h"
  8. #include <iostream>
  9. #include <vector>
  10. #include <algorithm>
  11. using namespace std;
  12.  
  13. int main() {
  14.   vector<Noisy> v;
  15.   v.reserve(11);
  16.   cout << "11 spaces have been reserved" << endl;
  17.   generate_n(back_inserter(v), 10, NoisyGen());
  18.   ostream_iterator<Noisy> out(cout, " ");
  19.   cout << endl;
  20.   copy(v.begin(), v.end(), out);
  21.   cout << "Inserting an element:" << endl;
  22.   vector<Noisy>::iterator it = 
  23.     v.begin() + v.size() / 2; // Middle
  24.   v.insert(it, Noisy());
  25.   cout << endl;
  26.   copy(v.begin(), v.end(), out);
  27.   cout << "\nErasing an element:" << endl;
  28.   // Cannot use the previous value of it:
  29.   it = v.begin() + v.size() / 2;
  30.   v.erase(it);
  31.   cout << endl;
  32.   copy(v.begin(), v.end(), out);
  33.   cout << endl;
  34. } ///:~
  35.